package com.kamistoat.meimeimall.common.interceptor;

import com.kamistoat.meimeimall.common.constant.AuthConstant;
import com.kamistoat.meimeimall.common.threadlocal.ThreadLocalContextHolder;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils;

/**
 * Dubbo 消费方拦截器。用于将消费方 ThreadLocal 信息添加到RPC调用中发送出去.
 * <p></p>
 * 顺序-49，比 {@link com.kamistoat.meimeimall.common.annotation.annotationHandler.LoginRequiredHandler} 级别低.
 */
@Order(-49)
@Activate
public class DubboConsumerInterceptor implements Filter {

    /**
     * Make sure call invoker.invoke() in your implementation.
     *
     * @param invoker
     * @param invocation
     */
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String loginSuccessToString = (String) ThreadLocalContextHolder.get(AuthConstant.LOGIN_USER_THREADLOCAL_NAME);
        if(!StringUtils.isEmpty(loginSuccessToString)){
            invocation.getAttachments().put(AuthConstant.LOGIN_USER_THREADLOCAL_NAME, loginSuccessToString);
        }
        return invoker.invoke(invocation);
    }
}
